package com.ziyia.a4_1_4;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

import com.ziyia.a4_1_4.pojo.User;

import java.io.Serializable;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // 跳转到下一个 Activity
    public void go1(View view) {
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }

    // 接收上一个 Activity 返回的数据
    public void go2(View view) {

        Intent intent = new Intent(this, MainActivity3.class);
        // 启动一个Activity，并希望返回一个结果
        startActivityForResult(intent, 200);
    }


    // 该方法接收上一个Activity返回的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            String result_data = data.getStringExtra("data");
            new AlertDialog.Builder(this)
                    .setTitle("接收到上一个Activity返回的信息")
                    .setMessage(result_data)
                    .create().show();
        }
    }

    public void go3(View view) {

        // 实体类数据
        User user = new User(1, "root", "root", 18);

        // 数据携带包
        Bundle bundle = new Bundle();
        // 写入一个可序列化的对象
        bundle.putSerializable("user", user);

        // 意图：启动一个Activity
        Intent intent = new Intent(this, MainActivity4.class);

        // 将我们的数据携带包封装到Intent中
        intent.putExtras(bundle);

        intent.putExtra("test", "test");

        intent.putExtra("test", "test2");

        // 启动 MainActivity4
        startActivity(intent);
    }
}